package com.lastproject.web.bean;

import java.text.DateFormat;
import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import com.lastproject.dto.ConfidentialEmployeeDTO;
import com.lastproject.dto.DepartmentDTO;
import com.lastproject.dto.RoleDTO;
import com.lastproject.dto.SearchUserDTO;
import com.lastproject.dto.TraineeDTO;
import com.lastproject.ejb.dao.ConfidentialEmployeeDAORemote;
import com.lastproject.ejb.dao.DepartmentDAORemote;
import com.lastproject.ejb.dao.RoleDAORemote;
import com.lastproject.ejb.dao.TraineeDAORemote;

@ManagedBean
@SessionScoped
public class SearchUserBean {

	// user table
	private String name;
	private String position;
	private String employmentType;
	
	//dates start here
	// from UI
	private Date startEmploymentDate;
	private Date endEmploymentDate;
	private Date startLastMedCheckDate;
	private Date endLastMedCheckDate;
	
	private String userType;
	private boolean enabledAccount = true;

	private boolean basicUser; // id 1
	private boolean teamLeader; // id 2
	private boolean administration; // id 3
	private boolean departmentManager; // id 4
	private boolean libraryAdmin; // id 5
	private boolean accountant; // id 7

	// employee table
	private String department;
	private String germanContactPers;
	private String careerLevel;

	// employee + trainee table
	private float startSalary;
	private float endSalary;

	private int selectedDepartmentId;
	private int selectedRoleId;

	@EJB
	DepartmentDAORemote departmentDAORemote;

	@EJB
	RoleDAORemote roleDAORemote;

	@EJB
	ConfidentialEmployeeDAORemote confidentialEmpDAO;

	@EJB
	TraineeDAORemote traineeDAOr;

	private List<DepartmentDTO> departmentList;
	private List<RoleDTO> roleList;

	private List<TraineeDTO> findTraineeList;
	private List<ConfidentialEmployeeDTO> findConfEmpList;
	private SearchUserDTO[] searchUserDTOList;

	public SearchUserBean() {

	}

	@PostConstruct
	public void populateLists() {
		departmentList = departmentDAORemote.getAll();
		roleList = roleDAORemote.findAll();
	}

	public int getSelectedDepartmentId() {
		return selectedDepartmentId;
	}

	public void setSelectedDepartmentId(int selectedDepartmentId) {
		this.selectedDepartmentId = selectedDepartmentId;
	}

	public int getSelectedRoleId() {
		return selectedRoleId;
	}

	public void setSelectedRoleId(int selectedRoleId) {
		this.selectedRoleId = selectedRoleId;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPosition() {
		return position;
	}

	public void setPosition(String position) {
		this.position = position;
	}

	public String getEmploymentType() {
		return employmentType;
	}

	public void setEmploymentType(String employmentType) {
		this.employmentType = employmentType;
	}

	// dates start here	
	
	public float getStartSalary() {
		return startSalary;
	}

	public Date getStartEmploymentDate() {
		return startEmploymentDate;
	}

	public void setStartEmploymentDate(Date startEmploymentDate) {
		this.startEmploymentDate = startEmploymentDate;
	}

	public Date getEndEmploymentDate() {
		return endEmploymentDate;
	}

	public void setEndEmploymentDate(Date endEmploymentDate) {
		this.endEmploymentDate = endEmploymentDate;
	}

	public Date getStartLastMedCheckDate() {
		return startLastMedCheckDate;
	}

	public void setStartLastMedCheckDate(Date startLastMedCheckDate) {
		this.startLastMedCheckDate = startLastMedCheckDate;
	}

	public Date getEndLastMedCheckDate() {
		return endLastMedCheckDate;
	}

	public void setEndLastMedCheckDate(Date endLastMedCheckDate) {
		this.endLastMedCheckDate = endLastMedCheckDate;
	}

	public void setStartSalary(float startSalary) {
		this.startSalary = startSalary;
	}

	public float getEndSalary() {
		return endSalary;
	}

	public void setEndSalary(float endSalary) {
		this.endSalary = endSalary;
	}

	public String getDepartment() {
		return department;
	}

	public void setDepartment(String department) {
		this.department = department;
	}

	public String getGermanContactPers() {
		return germanContactPers;
	}

	public void setGermanContactPers(String germanContactPers) {
		this.germanContactPers = germanContactPers;
	}

	public String getCareerLevel() {
		return careerLevel;
	}

	public void setCareerLevel(String careerLevel) {
		this.careerLevel = careerLevel;
	}

	public boolean isEnabledAccount() {
		return enabledAccount;
	}

	public void setEnabledAccount(boolean enabledAccount) {
		this.enabledAccount = enabledAccount;
	}

	public boolean isBasicUser() {
		return basicUser;
	}

	public void setBasicUser(boolean basicUser) {
		this.basicUser = basicUser;
	}

	public boolean isTeamLeader() {
		return teamLeader;
	}

	public void setTeamLeader(boolean teamLeader) {
		this.teamLeader = teamLeader;
	}

	public boolean isAdministration() {
		return administration;
	}

	public void setAdministration(boolean administration) {
		this.administration = administration;
	}

	public boolean isDepartmentManager() {
		return departmentManager;
	}

	public void setDepartmentManager(boolean departmentManager) {
		this.departmentManager = departmentManager;
	}

	public boolean isLibraryAdmin() {
		return libraryAdmin;
	}

	public void setLibraryAdmin(boolean libraryAdmin) {
		this.libraryAdmin = libraryAdmin;
	}

	public boolean isAccountant() {
		return accountant;
	}

	public void setAccountant(boolean accountant) {
		this.accountant = accountant;
	}

	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	public List<DepartmentDTO> getDepartmentList() {
		return departmentList;
	}

	public void setDepartmentList(List<DepartmentDTO> departmentList) {
		this.departmentList = departmentList;
	}

	public List<RoleDTO> getRoleList() {
		return roleList;
	}

	public void setRoleList(List<RoleDTO> roleList) {
		this.roleList = roleList;
	}

	public SearchUserDTO[] getSearchUserDTOList() {
		return searchUserDTOList;
	}

	public void setSearchUserDTOList(SearchUserDTO[] searchUserDTOList) {
		this.searchUserDTOList = searchUserDTOList;
	}

	public List<TraineeDTO> getFindTraineeList() {
		return findTraineeList;
	}

	public void setFindTraineeList(List<TraineeDTO> findTraineeList) {
		this.findTraineeList = findTraineeList;
	}

	public List<ConfidentialEmployeeDTO> getFindConfEmpList() {
		return findConfEmpList;
	}

	public void setFindConfEmpList(List<ConfidentialEmployeeDTO> findConfEmpList) {
		this.findConfEmpList = findConfEmpList;
	}

	public String findByCriteria() {
		searchUserDTOList = null;
		
		System.out.println("Dates from UI");
		System.out.println("startEmploymentDate " + startEmploymentDate);
		System.out.println("endEmploymentDate " + endEmploymentDate);
		System.out.println("startLastMedCheckDate " + startLastMedCheckDate);
		System.out.println("endLastMedCheckDate " + endLastMedCheckDate);

		if (basicUser) {
			System.out.println("basicUserRole selected");
			selectedRoleId = 1;
		}

		if (teamLeader) {
			System.out.println("teamLeaderRole selected");
			selectedRoleId = 2;
		}

		if (administration) {
			System.out.println("administrationRole selected");
			selectedRoleId = 3;
		}

		if (departmentManager) {
			System.out.println("departmentManagerRole selected");
			selectedRoleId = 4;
		}

		if (libraryAdmin) {
			System.out.println("libraryAdminRole selected");
			selectedRoleId = 5;
		}

		if (accountant) {
			System.out.println("accountantRole selected");
			selectedRoleId = 7;
		}


		if (userType.equals("ConfidentialEmployee")) {
			
			System.out.println("Searching Employees");
					
			findConfEmpList = confidentialEmpDAO.findByCriteria(name, position,
					employmentType, startEmploymentDate, endEmploymentDate,
					startLastMedCheckDate, endLastMedCheckDate, enabledAccount,
					selectedRoleId, selectedDepartmentId, germanContactPers,
					careerLevel, startSalary, endSalary);

			searchUserDTOList = new SearchUserDTO[findConfEmpList.size()];
			for (int i = 0; i < findConfEmpList.size(); i++) {
				searchUserDTOList[i] = new SearchUserDTO(findConfEmpList.get(i));
			}


		} else if (userType.equals("Trainee")) {
			System.out.println("Searching Trainees");

			findTraineeList = traineeDAOr.findByCriteria(name, position,
					employmentType, startEmploymentDate, endEmploymentDate,
					startLastMedCheckDate, endLastMedCheckDate, enabledAccount,
					startSalary, endSalary);

			searchUserDTOList = new SearchUserDTO[findTraineeList.size()];
			for (int i = 0; i < findTraineeList.size(); i++) {
				searchUserDTOList[i] = new SearchUserDTO(findTraineeList.get(i));
			}


		} else {
			
			System.out.println("Searching all ");

			findConfEmpList = confidentialEmpDAO.findByCriteria(name, position,
					employmentType, startEmploymentDate, endEmploymentDate,
					startLastMedCheckDate, endLastMedCheckDate, enabledAccount,
					selectedRoleId, selectedDepartmentId, germanContactPers,
					careerLevel, startSalary, endSalary);
			
//			for(int i=0 ; i<findConfEmpList.size() ; i++) {
//				System.out.println("SearchUserBean main else - employees: " + findConfEmpList.get(i).getLastname()
//						+" "+ findConfEmpList.get(i).getFirstname());
//			}
			

			if (!(selectedRoleId != 0 || selectedDepartmentId != 0
					|| (!germanContactPers.isEmpty()) || (!careerLevel
						.isEmpty()))) {
				findTraineeList = traineeDAOr.findByCriteria(name, position,
						employmentType, startEmploymentDate, endEmploymentDate,
						startLastMedCheckDate, endLastMedCheckDate,
						enabledAccount, startSalary, endSalary);

				// Adding ConfidentialEmployee list
				searchUserDTOList = new SearchUserDTO[findConfEmpList.size()
						+ findTraineeList.size()];

				for (int i = 0; i < findConfEmpList.size(); i++) {
					searchUserDTOList[i] = new SearchUserDTO(
							findConfEmpList.get(i));
//					System.out.println("Any - trainee - for1 "
//							+ searchUserDTOList[i].getLastname() + " "
//							+ searchUserDTOList[i].getFirstname());
				}
				// Adding Trainee list
				for (int i = findConfEmpList.size(); i < (findConfEmpList
						.size() + findTraineeList.size()); i++) {
					searchUserDTOList[i] = new SearchUserDTO(
							findTraineeList.get(i - findConfEmpList.size()));
					System.out.println("Any - trainee - for2 "
							+ searchUserDTOList[i].getLastname() + " "
							+ searchUserDTOList[i].getFirstname());
				}

			} else {
				// Adding ConfidentialEmployee list
				searchUserDTOList = new SearchUserDTO[findConfEmpList.size()];
				for (int i = 0; i < findConfEmpList.size(); i++) {
					searchUserDTOList[i] = new SearchUserDTO(
							findConfEmpList.get(i));
//					System.out.println("Any - trainee - else "
//							+ searchUserDTOList[i].getLastname() + " "
//							+ searchUserDTOList[i].getFirstname());
				}
			}
		}

		System.out.println("SearchUserBean - DTO list: ");
		for (int i = 0; i < searchUserDTOList.length; i++) {
			System.out.println("  -  " + searchUserDTOList[i].getLastname()
					+ searchUserDTOList[i].getFirstname());
		}

		return "searchUserResults";
	}

}
